<?php
import('com.yamvc.view.Mediator');
import("com.yamvc.event.EventDispatcher");
import("com.yamvc.event.Event");
import("com.yamvc.event.EventListener");
class View {
	/**
	 * Enter description here...
	 *
	 * @var View
	 */
	private static $_instance;
	/**
	 * Enter description here...
	 *
	 * @return View
	 */
	public static function instance(){
		if(self::$_instance==null){
			self::$_instance = new View();
		}
		return self::$_instance;
	}
	private function __construct(){
		
	}
	private $_helpersClassPath=array("com.yamvc.view.helpers");
	public function addHelperPath($path){
		$this->_helpersClassPath[] = $path;
	}
	private $_helpers = array();
	public function getHelper($class){
		if (isset($this->_helpers[$class])){
			return $this->_helpers[$class];
		}
		foreach ($this->_helpersClassPath as $path){
			try{
				import($path.".".$class);
				$helper = new $class();
				$this->_helpers[$class] = $helper;
				return $helper;
			}catch (ClassNotFoundException $e){}
		}
		return null;
	}
	private $_mediators=array();
	/**
	 * Enter description here...
	 *
	 * @var IEngine
	 */
	private $_renderer;
	/**
	 * Enter description here...
	 *
	 * @param IEngine $renderer
	 * @return IEngine
	 */
	public function setRenderer(IEngine $renderer){
		$this->_renderer = $renderer;
		return $this->_renderer;
	}
	/**
	 * Registers proxy with unique name
	 *
	 * @param string $mediatorName
	 * @param Mediator $mediator
	 * @return View
	 */
	public function registerMediator($mediatorName,Mediator $mediator){
		if (key_exists($mediatorName,$this->_mediators)){
			throw new Exception("Mediator with name ".$mediatorName."already registered");
		}
		$this->_mediators[$mediatorName] = $mediator;
		return $this;
	}
	/**
	 * Enter description here...
	 *
	 * @param unknown_type $mediatorName
	 * @return Mediator
	 */
	public function retreaveMediator($mediatorName){
		if(!key_exists($mediatorName,$this->_mediators)){
			throw new Exception("Mediator with this name not registered");
		}
		return $this->_mediators[$mediatorName];
	}
	
	public function registerTemplates(DOMNodeList $templates){
	for ($i=0;$i<$templates->length;$i++){
			$template = $templates->item($i);
			$templateInstance=View::instance()->createTemplate($template->getElementsByTagName("file")->item(0)->nodeValue);
			$parent = $template->getElementsByTagName("parent")->item(0);
			$parentName = $parent->getElementsByTagName("name")->item(0)->nodeValue;
			$parentVarName = $parent->getElementsByTagName("var")->item(0)->nodeValue;
			View::instance()->getTemplate($parentName)->putVariable($parentVarName,$templateInstance);
		}
	}
	
	public function registerMediators(DOMNodeList $mediators){
		foreach ($mediators as $mediatorConf){
			Logger::getLogger("Application")->log("Registering mediator");
			$mediatorClass=$mediatorConf->getElementsByTagName("class")->item(0)->nodeValue;
			$mediatorTemplateName=$mediatorConf->getElementsByTagName("templateName")->item(0)->nodeValue;
			$template = View::instance()->getTemplate($mediatorTemplateName);
			try{
				import($mediatorClass);
				$mediatorClass = explode(".",$mediatorClass);
				$mediator = $mediatorClass[sizeof($mediatorClass)-1];
				$mediatorInstance = new $mediator($template);
				$params=$mediatorConf->getElementsByTagName("param");
				for($i=0;$i<$params->length;$i++){
					$param = $params->item($i); 
					$paramName=$param->attributes->item(0)->value;
					$paramValue=$param->nodeValue;
					$mediatorInstance->setParam($paramName,$paramValue);
				}
				View::instance()->registerMediator($mediatorConf->attributes->item(0)->value,$mediatorInstance);
			}catch (ClassNotFoundException $e){
				echo "EXCEPTION!!!";
			}
			//var_dump($mediatorClass);
			//var_dump($mediatorTemplateName);
		}
	}
	 
	//// PROXY FUNCTIONS ////
	public function addEventListener($eventType,EventListener $eventListener){
		Controller::instance()->addEventListener($eventType,$eventListener);
	}
	public function dispatchEvent(Event $event){
		Controller::instance()->dispatchEvent($event);
	}
	
	/**
	 * Enter description here...
	 *
	 * @param string $name
	 * @return ITemplate
	 */
	public function createTemplate($name){
		return $this->_renderer->createTemplate($name);
	}
	
/**
	 * Enter description here...
	 *
	 * @param string $name
	 * @return ITemplate
	 */
	public function createRootTemplate($name){
		return $this->_renderer->createRootTemplate($name);
	}
	
	public function render(){
		$this->_renderer->render();
	}
	
	/**
	 * Enter description here...
	 *
	 * @param string $name
	 * @return ITemplate
	 */
	public function getTemplate($name){
		return $this->_renderer->getTemplate($name);
	}
	/**
	 * Enter description here...
	 * @return ITemplate
	 */
	public function getRootTemplate(){
		return $this->_renderer->getRootTemplate();
	}
	
	public function addScript($name){
		return $this->_renderer->addScript($name);
	}
}

?>